extends Path3D

@export var enemy_path: PackedScene
@export var difficulty_manager: Node
@export var victory_layer: CanvasLayer
@onready var timer: Timer = $Timer

func _ready() -> void:
	Engine.time_scale = 6

func spwan_enemmy() -> void:
	var new_enemy = enemy_path.instantiate()#注意 这个方法只有packedscene才有
	new_enemy.max_health = difficulty_manager.get_enemy_health()
	add_child(new_enemy)
	new_enemy.tree_exited.connect(player_victory)


func _on_timer_timeout() -> void:
	spwan_enemmy()
	timer.wait_time = difficulty_manager.get_spawn_time()


func _on_difficulty_manager_stop_spawning_enemies() -> void:
	timer.stop()

func player_victory() -> void:
	if timer.is_stopped():
		for child in get_children():
			if child is PathFollow3D:
				return
		victory_layer.victory()
			
